package game.common;

import game.constants.GameConstant;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

/**
 * 根据几率 计算 是否命中
 * 
 * @author serv_dev
 * 
 */
public class GenerateProbability {
	public static final short wanLv = 10000;// 总概率
	public static final short qianLv = 1000;// 总概率
	public static final Random random = new Random();

	// private static Logger logger = Logger.getLogger("OnlineLog");

	/**
	 * 
	 * 强化成功率最新计算公式：
	 * 
	 * @param min
	 * @param max
	 * @param now
	 * @param xyfNum
	 * @return
	 */
	public static boolean isSuccess(int min, int max, int now, int relLv, int xyfNum) {
		// 第一阶段： 0<当前强化次数<=最小强化次数
		// 强化成功率=（真实概率+幸运符所增加的成功率）*0.4*｛当前强化次数/（最小强化次数+最大强化次数）｝
		// 第二阶段：最小强化次数<当前强化次数<=最小强化次数*2
		// 强化成功率=真实概率+当前强化次数*2+幸运符所增加的成功率*【当前强化次数/（2*最小强化次数）】、
		// 第三阶段：当前强化次数 >2*最小强化次数
		// 强化成功率=【真实概率+幸运符增加的成功率+当前强化次数*2】
		boolean tf = false;
		double lv = 0;
		if (now <= 0) {
			now = 1;
		}
		if (now > 0 && now <= min) {
			lv = (relLv + xyfNum * GameConstant.XingYunJinglv) * 0.4 * (now * 1.0 / (min + max));
		} else if (now > min && now <= min * 2) {
			lv = relLv + now * 2 + xyfNum * GameConstant.XingYunJinglv * (now * 1.0 / (2 * min));
		} else if (now > 2 * min) {
			lv = relLv + xyfNum * GameConstant.XingYunJinglv + now * 2;
		} else if (now == max) {
			lv = wanLv;
		}
		// System.out.println(min + "," + max + "," + now + "," + relLv + "," + xyfNum);
		if (lv == wanLv || defaultIsGenerate((int) lv)) {
			tf = true;
		}
		// System.out.println(lv + "," + tf);
		return tf;
	}

	public static void main(String[] args) {
		GenerateProbability.isSuccess(10, 80, 80, 500, 0);
	}

	/**
	 * 根据几率 计算是否生成
	 * 
	 * @param probability
	 * @return true表示成功
	 */
	public static boolean isGenerate(int probability, int gailv) {
		if (gailv == 0) {
			gailv = 1000;
		}
		int random_seed = random.nextInt(gailv + 1);
		return probability >= random_seed;
	}

	/**
	 * 
	 * gailv/probability 比率形式
	 * 
	 * @param probability
	 * @param gailv
	 * @return
	 */
	public static boolean isGenerate2(int probability, int gailv) {
		if (probability == gailv)
			return true;
		if (gailv == 0) {
			gailv = 1;
		}
		int random_seed = random.nextInt(probability);
		return random_seed + 1 <= gailv;
	}

	/**
	 * 根据几率 计算是否生成
	 * 
	 * @param probability
	 * @return
	 */
	public static boolean defaultIsGenerate(int probability) {
		int random_seed = random.nextInt(wanLv);
		return probability >= random_seed;
	}

	/**
	 * 从 min 和 max 中间随机一个值
	 * 
	 * @param max
	 * @param min
	 * @return 包含min max
	 */
	public static int randomValue(int max, int min) {
		int temp = max - min;
		temp = GenerateProbability.random.nextInt(temp + 1);
		temp = temp + min;
		return temp;
	}

	/**
	 * 返回在0-maxcout之间产生的随机数时候小于num
	 * 
	 * @param num
	 * @return
	 */
	public static boolean isGenerateToBoolean(int num, int maxcout) {
		double count = Math.random() * maxcout;
		if (count < num) {
			return true;
		}
		return false;
	}

	/**
	 * 返回在0-maxcout之间产生的随机数时候小于num
	 * 
	 * @param num
	 * @return
	 */
	public static boolean isGenerateToBoolean(float num, int maxcout) {
		double count = Math.random() * maxcout;
		if (count < num) {
			return true;
		}
		return false;
	}

	/**
	 * 随机产生min到max之间的整数值 包括min max
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int randomIntValue(int min, int max) {
		return (int) (Math.random() * (double) (max - min + 1)) + min;
	}

	public static float randomFloatValue(float min, float max) {
		return (float) (Math.random() * (double) (max - min)) + min;
	}

	@SuppressWarnings("rawtypes")
	public static Object randomItem(Collection collection) {
		if (collection == null || collection.size() == 0) {
			return null;
		}
		int t = (int) (collection.size() * Math.random());
		int i = 0;
		for (Iterator item = collection.iterator(); i <= t && item.hasNext();) {
			Object next = item.next();
			if (i == t) {
				return next;
			}
			i++;
		}
		return null;
	}

	/**
	 * 半开区间[mn,mx)
	 * 
	 * @param mn
	 * @param mx
	 * @return
	 */
	public static int rdmValue(int mn, int mx) {
		int diff = mx - mn;
		int v = random.nextInt(diff);
		return v + mn;
	}

	/**
	 * 根据给定的数据随机这些数字
	 * 
	 * @param mn
	 * @return
	 */

	public static int rdmValue(int... mn) {
		int v = random.nextInt(mn.length);
		return mn[v];
	}

}
